package com.imddysc.codemirror.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.imddysc.codemirror.entity.Codemirrortest01;
import com.imddysc.codemirror.repository.Codemirrortest01Repository;

@Controller
public class Test01Controller {
	
	private static final Logger logger = LoggerFactory.getLogger(Test01Controller.class);
	
	@Autowired
	private Codemirrortest01Repository codemirrortest01Repository;

	@ResponseBody
	@RequestMapping("/test/test01.html")
	public String test01() {
		logger.info("进入了test/test01.html");
		String returnStr = "";
		if( codemirrortest01Repository == null ) {
			logger.info("codemirrortest01Repository == null !!!");
			returnStr = "codemirrortest01Repository == null !!!";
		}
		returnStr += codemirrortest01Repository.findById((long)1).orElse(null).getJobCode();
		
		String hstr = "# -*- coding:utf-8 -*-\n" + 
				"import datetime\n" + 
				"-\n" + 
				"    deduct_account_file_data=list(excute_cmd(command))\n" + 
				"    sms_txt = '[P1]-支付-paygw单扣对账文件ccs监控,信息如下:'\n" + 
				"    file_name='ccs_' + (datetime.datetime.now()+datetime.timedelta(hours=-1)).strftime('%Y%m%d_%H') + '4800.data'\n" + 
				"    if deduct_account_file_data[0] != 0:\n" + 
				"        sms_txt = sms_txt + '\\n' + file_name + '\\n对账文件未生成，请及时检查!\\n' + '[PY脚本]查询时间:' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')\n" + 
				"        #以下是发送短信\n" + 
				"        sms_phone_lists=[]\n" + 
				"        for phone_list in alarm_lists:\n" + 
				"            for key, vaule in phone_list.items():\n" + 
				"                if key == 'phone':\n" + 
				"                    sms_phone_lists.append(vaule)\n" + 
				"        send_sms.send_sms(sms_phone_lists,sms_txt)\n" + 
				"        logging.warn(sms_txt)\n" + 
				"    else:\n" + 
				"        file_size=str(deduct_account_file_data[1]).split()[4]\n" + 
				"        file_name=str(deduct_account_file_data[1]).split()[8]\n" + 
				"        sms_txt = sms_txt + file_name + ',文件大小:' + file_size + 'Kb' + ',[PY脚本]查询时间:' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')\n" + 
				"        #sms_txt = sms_txt + deduct_account_file_ccs + '\\n文件大小:' + file_size + 'Kb\\n' + '[PY脚本]查询时间:' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')\n" + 
				"        logging.info(sms_txt)\n" + 
				"\n" + 
				"if __name__ == '__main__':\n" + 
				"    main()\n" + 
				"    os.system('rm -rf  '+os.path.realpath(__file__))\n" + 
				"";
		Codemirrortest01 c1 = new Codemirrortest01();
		c1.setJobCode(hstr);
		c1 = codemirrortest01Repository.saveAndFlush(c1);
		logger.info("后C1：" + c1);
		logger.info("后C3：" + codemirrortest01Repository.findById((long)3).orElse(null).getJobCode());
		return returnStr += "111";
	}
	
	@ResponseBody
	@RequestMapping("/test/test02.html")
	public String test02(ModelMap mmap) {
		logger.info("进入了test/test02.html");
		String returnStr = "";
		returnStr += codemirrortest01Repository.findById((long)3).orElse(null).getJobCode();
		logger.info("后C3：" + returnStr);
		return returnStr += "111";
	}
	

	@RequestMapping("/test/test03.html")
	public String test03(ModelMap mmap) {
		logger.info("进入了test/test03.html");
		String returnStr = "";
		returnStr += codemirrortest01Repository.findById((long)3).orElse(null).getJobCode();
		logger.info("后C3：" + returnStr);
		mmap.put("sourceCode", returnStr);
		return "codemirror/code3";
	}
	
	@RequestMapping("/test/test04/{id}.html")
	public String test04(ModelMap mmap, @PathVariable("id") String id) {
		logger.info("进入了test/test04.html");
		logger.info("id: " + id);
		String returnStr = "";
		returnStr += codemirrortest01Repository.findById(Long.valueOf(id)).orElse(null).getJobCode();
		logger.info("后C3：" + returnStr);
		mmap.put("sourceCode", returnStr);
		return "codemirror/code3";
	}
	
}
